package com.hwpt.chesedemo.presentation.component

import androidx.compose.runtime.Immutable

@Immutable
data class GameState(
    val currentScreen: ChessScreen = ChessScreen.DIFFICULTY_SELECTION,
    val selectedDifficulty: Difficulty? = null,
    val currentTurn: Player = Player.WHITE,
    val gameStatus: GameStatus = GameStatus.IN_PROGRESS,
    val moveCount: Int = 0,
    val moveHistory: List<ChessMove> = emptyList(),
    val boardState: List<List<ChessPiece?>> = initialBoardState(),
    val showGameOverDialog: Boolean = false,
    val gameResult: GameResult? = null,
    val selectedPiece: Position? = null, // 添加选中的棋子位置
    val validMoves: List<Position> = emptyList(), // 有效的移动位置
    val highlightedMove: Pair<Position, Position>? = null // 高亮显示的移动

)

enum class ChessScreen {
    DIFFICULTY_SELECTION, GAME_BOARD
}

enum class Player {
    WHITE, BLACK
}

enum class GameStatus {
    IN_PROGRESS, CHECK, CHECKMATE, STALEMATE, RESIGNED, DRAW
}

data class GameResult(
    val title: String,
    val message: String,
    val winner: Player? = null
)

data class ChessMove(
    val moveNumber: Int,
    val whiteMove: String? = null,
    val blackMove: String? = null
)

@Immutable
data class Difficulty(
    val level: String, // 保持不变，用于内部标识
    val nameKey: String, // 改为键名而不是具体值
    val descriptionKey: String, // 改为键名
    val iconKey: String, // 改为键名
    val aiLevel: Int
)

// 注意：difficulties 列表现在在 AppStrings.getDifficulties() 中定义

@Immutable
data class ChessPiece(
    val type: PieceType,
    val player: Player,
    val position: Position
)

@Immutable
data class Position(
    val row: Int,
    val col: Int
)

enum class PieceType {
    PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING
}

fun initialBoardState(): List<List<ChessPiece?>> {
    return List(8) { row ->
        List(8) { col ->
            when (row) {
                0 -> when (col) {
                    0, 7 -> ChessPiece(PieceType.ROOK, Player.BLACK, Position(row, col))
                    1, 6 -> ChessPiece(PieceType.KNIGHT, Player.BLACK, Position(row, col))
                    2, 5 -> ChessPiece(PieceType.BISHOP, Player.BLACK, Position(row, col))
                    3 -> ChessPiece(PieceType.QUEEN, Player.BLACK, Position(row, col))
                    4 -> ChessPiece(PieceType.KING, Player.BLACK, Position(row, col))
                    else -> null
                }
                1 -> ChessPiece(PieceType.PAWN, Player.BLACK, Position(row, col))
                6 -> ChessPiece(PieceType.PAWN, Player.WHITE, Position(row, col))
                7 -> when (col) {
                    0, 7 -> ChessPiece(PieceType.ROOK, Player.WHITE, Position(row, col))
                    1, 6 -> ChessPiece(PieceType.KNIGHT, Player.WHITE, Position(row, col))
                    2, 5 -> ChessPiece(PieceType.BISHOP, Player.WHITE, Position(row, col))
                    3 -> ChessPiece(PieceType.QUEEN, Player.WHITE, Position(row, col))
                    4 -> ChessPiece(PieceType.KING, Player.WHITE, Position(row, col))
                    else -> null
                }
                else -> null
            }
        }
    }
}